package com.jinke.api.modules.app.controller;


import com.jinke.api.common.util.Global;
import com.jinke.api.modules.api.conf.ApiUrlsConfig;
import com.jinke.api.modules.app.util.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 承运商平台 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-01-15
 */
@RestController
@Api(tags = "静态")
@Tag(name = "静态", description = "静态")
@RequestMapping("/app/static")
@Slf4j
public class StaticController {

    @Resource
    private ApiUrlsConfig apiUrlsConfig;
    @GetMapping("/download")
    public ResponseEntity<ByteArrayResource> downloadZipFile(String fileName) throws IOException {
        // 文件存储路径，假设 ZIP 文件已存储到 resources 目录下
        String tempDir = System.getProperty("java.io.tmpdir");
        String localFilePath = tempDir + File.separator + fileName;
        File file = new File(localFilePath);
        Path path = Paths.get(file.getAbsolutePath());
        ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+fileName);

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);
    }
    @NotNull
    public String exports(List userRechargeExports, Class clazz) {
        String tempDir = System.getProperty("java.io.tmpdir");
        // 创建临时文件名
        String fileName = LocalDateTime.now().toString().replace(":", "_") + ".xlsx";

// 拼接临时文件的完整路径
        String localFilePath = tempDir + File.separator + fileName;
        System.out.println(localFilePath);

// 创建临时文件
        File tempFile = new File(localFilePath);
        try {
            tempFile.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

// 将数据从 response.getOutputStream() 复制到本地文件输出流
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(localFilePath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        EasyExcel.write(fileOutputStream,clazz).sheet("datas").doWrite(userRechargeExports);
        String s = Global.DOWNLOAD_BY_FILE_NAME_API + fileName;
        return s;
    }

}

